package com.hzhr.iot.core.util;

import java.util.Random;
import java.util.UUID;

public class HzhrStringUtils {
    /**
     * object对象转字符串
     * @param object
     * @return
     */
    public static String obj2str(Object object) {
        if (object == null) return null;
        return object.toString();
    }

    /**
     * 字符串左补0
     * @param str 原始字符串
     * @param size 补充0之后的长度
     */
    public static String fillZeroForLeft(String str, int size) {
        int strSize =  str.length();
        if (strSize < size) {
            int zeroCount = size - strSize;
            for (int i = 0; i < zeroCount; i++) {
                str = "0" + str;
            }
        }
        return str;
    }

    /**
     * 首字母大写
     * @param str
     * @return
     */
    public static String firstCharToUppserCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (i == 0) {
                sb.append(Character.toUpperCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * 首字母小写
     * @param str
     * @return
     */
    public static String firstCharToLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (i == 0) {
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    public static String firstCharToLowerCase1(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        boolean isFirst = true;
        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (isFirst) {
                sb.append(Character.toLowerCase(c));
                isFirst = false;
            } else {
                sb.append(c);
            }
            if (c == '_') {
                isFirst = true;
            }
        }

        return sb.toString();
    }

    /**
     * 字符串转小写
     * @param str
     * @return
     */
    public static String strToLowerCase(String str) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (Character.isUpperCase(c)) {
                sb.append(Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * 隔断驼峰字符串
     * @param str
     * @param regex
     * @return
     */
    public static String splitHumpStr(String str, String regex) {
        StringBuilder sb = new StringBuilder();
        char[] cs = str.toCharArray();

        for (int i = 0; i < cs.length; i++) {
            char c = cs[i];
            if (i == 0) {
                sb.append(c);
                continue;
            }
            if (Character.isUpperCase(c)) {
                sb.append(regex + Character.toLowerCase(c));
            } else {
                sb.append(c);
            }
        }

        return sb.toString();
    }

    /**
     * 生成GUID值
     * @return  GUID
     */
    public static String getGUID(){
        UUID uuId =  java.util.UUID.randomUUID();
        return uuId.toString();
    }

    /**
     * 字符串左补0
     *
     * @param str
     *            原始字符串
     * @param size
     *            补充0之后的长度
     */
    public static String FillZeroForLeft(String str, int size) {
        if (str == null) return null;
        int strSize = str.length();
        if (strSize < size) {
            int zeroCount = size - strSize;
            for (int i = 0; i < zeroCount; i++) {
                str = "0" + str;
            }
        }
        return str;
    }

    /**
     * 字符串左补0
     *
     * @param str
     *            原始字符串
     * @param size
     *            补充0之后的长度
     */
    public static String FillZeroForRight(String str, int size) {
        if (str == null) return null;
        int strSize = str.length();
        if (strSize < size) {
            int zeroCount = size - strSize;
            for (int i = 0; i < zeroCount; i++) {
                str = str + "0";
            }
        }
        return str;
    }

    public static void main(String[] args) {
        Random rand =new Random();
        System.out.println(rand.nextInt(100));
    }
}
